<template>
  <div class="admin-page">
    <el-card shadow="never" class="admin-card">
      <div class="card-header">
        {{ title }}
        <!-- <el-switch
          v-model="isAdvancedSearch"
          active-text="高级搜索"
          inactive-text="简单搜索"
          size="small"
          style="margin-left: 1rem;"
        /> -->
      </div>
      
      <!-- 筛选栏 -->
      <el-form :inline="true" :model="filters" class="filter-bar">
        <slot name="filters"></slot>
      </el-form>
      <!-- 表格区域 -->
      <div class="table-scroll">
        <slot name="table"></slot>
      </div>

      <!-- 分页器 -->
      <div class="pager">
        <slot name="pagination"></slot>
        <div v-if="paginationEnabled" class="simple-pager">
          <div class="pager-info">
            <span class="summary">总数：<strong>{{ total }}</strong></span>
            
          </div>
          <div class="pager-controls">
            <el-button 
              size="small" 
              @click="goPrevPage" 
              :disabled="isPrevDisabled"
              class="page-btn prev-btn"
            >
              <i class="el-icon-arrow-left"></i>
              上一页
            </el-button>
            <el-pagination
              background
              layout="prev, next"
              :current-page="page"
              @current-change="onPageChange"
              class="mini-pagination"
            />
            <el-button 
              size="small" 
              type="primary" 
              @click="goNextPage" 
              :disabled="isNextDisabled"
              class="page-btn next-btn"
            >
              下一页
              <i class="el-icon-arrow-right"></i>
            </el-button>
          </div>
          <span class="current-page">第 <strong>{{ page }}</strong> 页</span>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  title: string
  filters?: any
  paginationEnabled?: boolean
  page?: number
  total?: number
  pageSize?: number
}

const props = defineProps<Props>()
const emit = defineEmits<{
  (e: 'update:page', value: number): void
}>()


const page = computed(() => props.page ?? 1)
const total = computed(() => props.total ?? 10)
const pageSize = computed(() => props.pageSize ?? 1)
const paginationEnabled = computed(() => props.paginationEnabled === true)

const maxPage = computed(() => Math.max(1, Math.ceil(total.value / pageSize.value)))
const isPrevDisabled = computed(() => page.value <= 1)
const isNextDisabled = computed(() => page.value >= maxPage.value)

const onPageChange = (newPage: number) => {
  emit('update:page', newPage)
}

const goPrevPage = () => {
  if (page.value > 1) emit('update:page', page.value - 1)
}

const goNextPage = () => {
  if (page.value < maxPage.value) emit('update:page', page.value + 1)
}
</script>

<style scoped lang="scss">
.admin-page {
  min-height: calc(100vh - 2.5rem); /* 减去头部导航的高度 */
  max-width: 100%;
  margin: 0 auto;
  backdrop-filter: blur(0.3125rem); /* 5px = 0.3125rem */
  border-radius: 0.75rem; /* 12px = 0.75rem */
  box-shadow: 0 0.25rem 1.25rem rgba(135, 206, 235, 0.1); /* 4px 20px = 0.25rem 1.25rem */
}

.admin-card {
  
  border-radius: 0.8rem;
  box-shadow: inset 0 0 0 0.0625rem #eef1f6;
}

.card-header {
  font-size: 1.125rem;
  font-weight: 600;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
}

.advanced-search-tip {
  margin-bottom: 1rem;
}

.filter-bar {
  padding: 1rem;
  background: #f7f9fc;
  border-radius: 0.75rem;
  border: 0.0625rem solid #eef1f6;
  margin-bottom: 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 1rem;
  row-gap: 0.75rem;
  position: relative;
}

/* 筛选栏分组线 */
.filter-bar::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 0.0625rem;
  background: linear-gradient(90deg, transparent, #e4e7ed, transparent);
  opacity: 0.3;
  pointer-events: none;
}

.table-scroll {
  width: 100%;
  overflow-x: auto;
  text-align: center;
}

.pager {
  display: flex;
  justify-content: center;
  padding: 1.125rem 0 0.25rem;
}

.simple-pager {
  display: flex;
  gap: 1.5rem;
  padding: 1rem 1.5rem; 
}

.pager-info {
  display: flex;
}

.pager-info .summary {
  font-size: 0.875rem;
  color: #64748b;
  font-weight: 500;
}

.pager-info strong {
  color: #0f172a;
  font-weight: 600;
  margin-left: 0.25rem;
}

.pager-controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.page-btn {
  border-radius: 0.75rem;
  font-weight: 500;
  transition: all 0.2s ease;
  border: 0.0625rem solid transparent;
  min-width: 5rem;
}

.page-btn:hover:not(:disabled) {
  transform: translateY(-0.125rem);
  box-shadow: 0 0.25rem 0.75rem rgba(0, 0, 0, 0.15);
}

.page-btn.prev-btn {
  background: linear-gradient(135deg, #f8fafc 0%, #e2e8f0 100%);
  color: #475569;
  border-color: #cbd5e1;
}

.page-btn.prev-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #e2e8f0 0%, #cbd5e1 100%);
  color: #334155;
}

.page-btn.next-btn {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  border-color: #1d4ed8;
}

.page-btn.next-btn:hover:not(:disabled) {
  background: linear-gradient(135deg, #1d4ed8 0%, #1e40af 100%);
  transform: translateY(-0.125rem);
}

.page-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none !important;
  box-shadow: none !important;
}

.mini-pagination {
  margin: 0 0.5rem;
}

/* 自定义 Element Plus 分页器样式 */
:deep(.mini-pagination .el-pagination) {
  --el-pagination-bg-color: transparent;
  --el-pagination-text-color: #64748b;
  --el-pagination-border-radius: 0.5rem;
}

:deep(.mini-pagination .el-pagination .btn-prev),
:deep(.mini-pagination .el-pagination .btn-next) {
  background: transparent;
  border: 0.0625rem solid #e2e8f0;
  color: #64748b;
  border-radius: 0.5rem;
  transition: all 0.2s ease;
}

:deep(.mini-pagination .el-pagination .btn-prev:hover),
:deep(.mini-pagination .el-pagination .btn-next:hover) {
  background: #f1f5f9;
  border-color: #cbd5e1;
  color: #475569;
}

:deep(.mini-pagination .el-pagination .btn-prev:disabled),
:deep(.mini-pagination .el-pagination .btn-next:disabled) {
  opacity: 0.5;
  cursor: not-allowed;
}

/* 全局样式覆盖 */
:deep(.filter-item) {
  margin-bottom: 0 !important;
  margin-right: 0 !important;
  flex-shrink: 0;
  min-width: fit-content;
  transition: all 0.2s ease;
}

:deep(.filter-item:hover) {
  transform: translateY(-0.125rem);
}

:deep(.filter-item .el-form-item__label) {
  font-size: 0.875rem;
  color: #606266;
  font-weight: 500;
  white-space: nowrap;
  margin-bottom: 0.25rem;
  transition: color 0.2s ease;
}

:deep(.filter-item:hover .el-form-item__label) {
  color: #409eff;
}

:deep(.filter-item .el-input),
:deep(.filter-item .el-select) {
  width: 8.75rem;
}

:deep(.filter-item:last-child) {
  margin-left: auto;
  align-self: flex-end;
}

:deep(.filter-item:last-child .el-button) {
  margin-left: 0.5rem;
}

:deep(.list-table),
:deep(.order-table) {
  border-radius: 0.75rem;
  overflow: hidden;
}

:deep(.table-header) {
  background: #f7f9fc !important;
  color: #606266 !important;
  font-weight: 600 !important;
}

/* 表格内容居中 - 全局样式 */
:deep(.list-table .el-table__cell),
:deep(.order-table .el-table__cell) {
  text-align: center !important;
  vertical-align: middle !important;
}

:deep(.list-table th.el-table__cell),
:deep(.order-table th.el-table__cell) {
  text-align: center !important;
  background-color: #f5f7fa !important;
}

/* 特殊样式覆盖 */
:deep(.filter-item .el-input),
:deep(.filter-item .el-select) {
  &.narrow {
    width: 4rem;
  }
  
  &.wide {
    width: 12rem;
  }
  
  &.extra-wide {
    width: 16rem;
  }
}

:deep(.filter-item:last-child .el-button) {
  &.narrow {
    margin-left: 0.3rem;
  }
}

:deep(.date-picker-container) {
  width: 16rem;
}

:deep(.price-range-container) {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

:deep(.price-range-container .el-input) {
  width: 6rem;
}

:deep(.price-range-container .separator) {
  color: #909399;
  font-size: 0.875rem;
  white-space: nowrap;
}

:deep(.location-container) {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

:deep(.location-container .el-select) {
  width: 6rem;
}

:deep(.image-container) {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 3.125rem;
  height: 3.125rem;
}

:deep(.product-image) {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

:deep(.logo-container),
:deep(.avatar-container) {
  display: flex;
  justify-content: center;
  align-items: center;
}

/* 响应式布局 */
@media (max-width: 1200px) {
  .filter-bar {
    gap: 0.75rem;
    row-gap: 0.5rem;
  }
  
  :deep(.filter-item .el-input),
  :deep(.filter-item .el-select) {
    width: 7.5rem;
  }
  
  :deep(.date-picker-container) {
    width: 14rem;
  }
}

@media (max-width: 768px) {
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 0.5rem;
  }
  
  :deep(.filter-item) {
    width: 100%;
  }
  
  :deep(.filter-item .el-input),
  :deep(.filter-item .el-select) {
    width: 100%;
  }
  
  :deep(.filter-item:last-child) {
    margin-left: 0;
    align-self: stretch;
  }
  
  :deep(.date-picker-container) {
    width: 100%;
  }
  
  :deep(.price-range-container),
  :deep(.location-container) {
    width: 100%;
    justify-content: space-between;
  }

  .simple-pager {
    min-width: 20rem;
    padding: 1rem;
  }
  
  .pager-info {
    flex-direction: column;
    gap: 0.5rem;
    text-align: center;
  }
  
  .pager-controls {
    flex-wrap: wrap;
    justify-content: center;
  }
  
  .page-btn {
    min-width: 4.5rem;
    font-size: 0.8125rem;
  }
}
</style>
